package com.qtyx.nms.ui.base

import android.app.Activity
import android.util.Log
import com.ldd.base.util.ActivityManageUtil
import com.qtyx.nms.custom.LoadingPopup
import com.qtyx.nms.mvp.base.BasePresenter
import com.qtyx.nms.mvp.base.BaseView
import com.qtyx.nms.ui.activity.LoginActivity

/**
 * 集成网络请求
 */
abstract class BaseMVPActivity<P : BasePresenter> : BaseActivity(),BaseView {
    protected lateinit var presenter: P
    protected abstract fun createPresenter(): P
    override fun initPresenter() {
        presenter = createPresenter()
    }

    private var loadingPopup: LoadingPopup? = null
    override fun showLoading() {
        if (loadingPopup == null) {
            loadingPopup = LoadingPopup(mContext as Activity)
        }
        window.decorView.post { loadingPopup?.show() }
    }

    override fun hideLoading() {
        window.decorView.postDelayed({ loadingPopup?.dismiss() }, 300)
    }

    override fun onError(msg: String) {
        if(msg.isNotEmpty()){
            showToast(msg)
        }
    }

    var isFirstToLogin=true
    @Synchronized
    override fun onTokenShiXiao(msg: String) {
        if(isFirstToLogin){
            isFirstToLogin=false
            showToast(msg)
            ActivityManageUtil.finishAllActivity()
            startActivityCustom(LoginActivity::class.java)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.detachView()
    }

}